﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

using Jarvis.Analytics;

namespace Jarvis.Windows
{
    /// <summary>
    /// Interaction logic for PredictionsWindow.xaml
    /// </summary>
    public partial class PredictionsWindow : Window
    {
        public PredictionsWindow()
        {
            InitializeComponent();
        }

        private void UpdateLabel(object o)
        {
            Dictionary<String, String> d = (Dictionary<String, String>)o;

            foreach (String key in d.Keys)
            {
                String prediction = d[key];

                if (key.Equals("Activity"))
                    activityLabel.Content = prediction;
                else if (key.Equals("Location"))
                    locationLabel.Content = prediction;
                else if (key.Equals("Social"))
                    socialLabel.Content = prediction;
            }
        }

        public void PredictionUpdated(object sender, EventArgs ea, String key, String prediction)
        {
            Dictionary<String, String> d = new Dictionary<String, String>();
            d[key] = prediction;

            if (base.CheckAccess() == true)
                UpdateLabel(d);
            else
            {
                base.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new System.Threading.WaitCallback(UpdateLabel), d); 
            }
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
                (DispatcherOperationCallback)delegate(object o)
                {
                    Hide();
                    return null;
                }, null);


            AnalyticsManager.LogEvent("Window Closing", "Predictions");
            
            e.Cancel = true;
        }
    }
}
